/*
 * jPOS' miniSpace
 * Copyright (C) 2000-2006 Alejandro Revilla
 * jPOS.org (http://jpos.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 *
 */
package org.jpos.space;

import java.util.ArrayList;
import java.util.List;

/**
 * Space related helper methods
 *
 * @author Alejandro Revilla
 * @version $Revision: 2316 $ $Date: 2006-05-16 17:31:11 -0300 (Tue, 16 May 2006) $
 * @since 1.4.7
 */

public class SpaceUtil {
    /**
     * return all entries under a given key
     *
     * @param sp the Space
     * @param key Entry's key
     * @return array containing all entries under key
     */
    public static Object[] inpAll (Space sp, Object key) {
        List list = new ArrayList();
        Object value;
        do {
            value = sp.inp (key);
            if (value != null) {
                list.add (value);
            }
        } while (value != null);
        return list.toArray();
    }

    /**
     * Remove all entries under key
     *
     * @param sp the Space
     * @param key Entry's key
     */
    public static void wipe (Space sp, Object key) {
        while (sp.inp (key) != null)
            ;
    }
    public static long nextLong (Space sp, Object key) {
        long l = 0L;
        synchronized (sp) {
            Object obj = sp.inp (key);
            wipe (sp, key); // just in case
            if (obj instanceof Long) 
                l = ((Long)obj).longValue();
            sp.out (key, new Long (++l));
        }
        return l;
    }
}

